package com.columboid.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.util.IndentPrinter;

/**
 * Servlet implementation class HelloServlet
 */

public final class HelloServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2497875218682314994L;
	private Destination destination;
	private long sleepTime=200;
	private int messageSize = 255;
	private long timeToLive;
	private String user = ActiveMQConnection.DEFAULT_USER;
	private String password = ActiveMQConnection.DEFAULT_PASSWORD;
	private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
	private String subject = "FOO.BAR";
	private boolean topic;
	private boolean transacted;
	private boolean persistent;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HelloServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		hello(request, response);
		// sendMessageToQueue();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	private void hello(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		PrintWriter out = response.getWriter();
		sendMessageToQueue();
		out.println("Hello, world!");
		out.close();
	}

	private void sendMessageToQueue() {
		Connection connection = null;
		try {
			System.out.println("Connecting to URL: " + url);
			System.out.println("Publishing a Message with size " + messageSize
					+ " to " + (topic ? "topic" : "queue") + ": " + subject);
			System.out.println("Using "
					+ (persistent ? "persistent" : "non-persistent")
					+ " messages");
			System.out.println("Sleeping between publish " + sleepTime + " ms");
			if (timeToLive != 0) {
				System.out.println("Messages time to live " + timeToLive
						+ " ms");
			}

			// Create the connection.
			ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
					user, password, url);
			connection = connectionFactory.createConnection();
			connection.start();

			// Create the session
			Session session = connection.createSession(transacted,
					Session.AUTO_ACKNOWLEDGE);
			if (topic) {
				destination = session.createTopic(subject);
			} else {
				destination = session.createQueue(subject);
			}

			// Create the producer.
			MessageProducer producer = session.createProducer(destination);
			if (persistent) {
				producer.setDeliveryMode(DeliveryMode.PERSISTENT);
			} else {
				producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
			}
			if (timeToLive != 0) {
				producer.setTimeToLive(timeToLive);
			}

			TextMessage message = session.createTextMessage("Test Message");
			producer.send(message);

			Thread.sleep(sleepTime);

			System.out.println("Done.");

			// Use the ActiveMQConnection interface to dump the connection
			// stats.
			ActiveMQConnection c = (ActiveMQConnection) connection;
			c.getConnectionStats().dump(new IndentPrinter());
			

		} catch (Exception e) {
			System.out.println("Caught: " + e);
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (Throwable ignore) {
			}
		}
	}
}
